package com.sc.nft.admin.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.sc.nft.admin.request.*;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.entity.*;
import com.sc.nft.entity.vo.AdminSimpleCastCollectionVO;
import com.sc.nft.entity.vo.ecology.*;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.pool.SysMemberPool;
import com.sc.nft.service.AdminEcologyCastConfService;
import com.sc.nft.service.EcologyCastExchangeConfService;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/ecology/cast")
@Api(tags = "后台生态铸造配置管理")
public class AdminEcologyCastConfController {


    private final AdminEcologyCastConfService adminEcologyCastConfService;
    private final EcologyCastExchangeConfService ecologyCastExchangeConfService;
    private final Redisson redisson;


    @PostMapping(value = "/addAndUpdateCastConf", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "新增或者修改生态铸造配置", notes = "新增生态铸造配置", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result addAndUpdateCastConf(@Valid @RequestBody AddAndUpdateCastConfRequest request) {
        EcologyCastConf ecologyCastConf = BeanUtil.copyProperties(request, EcologyCastConf.class);
        adminEcologyCastConfService.addAndUpdateCastConf(ecologyCastConf,request.getCastWayList());
        return Result.success();
    }


    @PostMapping(value = "/getPageCastConf", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "分页获取生态铸造配置", notes = "分页获取生态铸造配置", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AdminGetPageCastConfVO> getPageCastConf(@Valid @RequestBody GetPageCastConfRequest request) {
        return PageResult.success(adminEcologyCastConfService.getPageCastConf(request.getCollectionName(),request.getPageNo(),request.getPageSize()));
    }

    @PostMapping(value = "/getCastConfDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取生态铸造配置详情", notes = "获取生态铸造配置详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<AdminCastConfDetailsVO> getCastConfDetails(@Valid @RequestBody IdRequest request) {
        return Result.success(adminEcologyCastConfService.getCastConfDetails(request.getId()));
    }

    @PostMapping(value = "/delExpendById", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除生态铸造消耗配置", notes = "删除生态铸造消耗配置", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result delExpendById(@Valid @RequestBody IdRequest request) {
        adminEcologyCastConfService.delExpendById(request.getId());
        return Result.success();
    }

    @PostMapping(value = "/updateCastConfStatusgById", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取铸造配置上下架", notes = "获取铸造配置上下架", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result updateCastConfStatusgById(@Valid @RequestBody StatusRequest request) {
        adminEcologyCastConfService.updateCastConfStatusgById(request.getId(),request.getStatus());
        return Result.success();
    }

    @PostMapping(value = "/updateCastConfSortById", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取铸造配置上下架", notes = "获取熔炼配置上下架", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result updateCastConfSortById(@Valid @RequestBody SortRequest request) {
        adminEcologyCastConfService.updateCastConfSortById(request.getId(),Long.valueOf(request.getSort()));
        return Result.success();
    }

    @PostMapping(value = "/delCastConfById", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除生态铸造配置", notes = "删除生态铸造配置", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result delCastConfById(@Valid @RequestBody IdRequest request) {
        adminEcologyCastConfService.delCastConfById(request.getId());
        return Result.success();
    }


    @PostMapping(value = "/getPageUserRecords", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "用户铸造记录奖励", notes = "用户熔炼记录奖励", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AdminEcologyCastUserRecordsVO> getPageUserRecordsRecords(@Valid @RequestBody UserEcologyHatchRcordsReuqest request) {
        return PageResult.success(adminEcologyCastConfService.getPageUserRecordsRecords(request.getCollectionName(),request.getUserTel(),request.getStartTime(),request.getEndTime(),request.getPageNo(),request.getPageSize()));
    }

    @PostMapping(value = "/getPageUserRecordsExport", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "用户铸造记录奖励导出", notes = "用户熔炼记录奖励导出", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<List<AdminEcologyCastUserRecordsVO>> getPageUserRecordsExport(@Valid @RequestBody UserEcologyHatchRcordsReuqest request) {
        return Result.success(adminEcologyCastConfService.getPageUserRecordsExport(request.getCollectionName(),request.getUserTel(),request.getStartTime(),request.getEndTime()));
    }


    @PostMapping(value = "/userEcologyCastUpperLimitDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "用户铸造上限记录(汇总)", notes = "用户铸造上限记录(汇总)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<List<AdminUserEcologyCastUpperLimitDetailsVO>> userEcologyCastUpperLimitDetails(@Valid @RequestBody AdminUserTelRequest request) {
        return Result.success(adminEcologyCastConfService.userEcologyCastUpperLimitDetails(request.getUserTel()));
    }

    @PostMapping(value = "/userEcologyCastUpperLimitPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "用户铸造上限记录(明细)", notes = "用户铸造上限记录(明细)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AdminUserEcologyCastUpperLimitPageVO> userEcologyCastUpperLimitPage(@Valid @RequestBody UserEcologyCastUpperLimitPageRequest request) {
        return PageResult.success(adminEcologyCastConfService.userEcologyCastUpperLimitPage(request.getUserId(),request.getCollectionId(),request.getBeginTime(),request.getEndTime(),request.getPageNo(),request.getPageSize()));
    }


    @PostMapping(value = "/getSimplePage", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "简单的铸造藏品列表", notes = "简单的铸造藏品列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AdminSimpleCastCollectionVO> getSimplePage(@Valid @RequestBody PageRequest request) {
        return PageResult.success(adminEcologyCastConfService.getSimplePage(request.getPageNo(),request.getPageSize()));
    }


//    @PostMapping(value = "/initCastUpperLimit", produces = MediaType.APPLICATION_JSON_VALUE)
//    @ApiOperation(value = "初始化铸造上限", notes = "初始化铸造上限", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
//    @SysMemberLogin
//    public Result initCastUpperLimit() {
//        adminEcologyCastConfService.initCastUpperLimit();
//        return Result.success();
//    }


    @PostMapping(value = "/addExchangeCastUpperLimit", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "新增兑换铸造上限", notes = "兑换铸造上限", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result addExchangeCastUpperLimit(@Valid @RequestBody AddExchangeCastUpperLimitRequest request) {
        EcologyCastExchangeConf ecologyCastExchangeConf = BeanUtil.copyProperties(request, EcologyCastExchangeConf.class);
        ecologyCastExchangeConfService.addExchangeCastUpperLimit(ecologyCastExchangeConf);
        return Result.success();
    }


    @PostMapping(value = "/dealExchangeCastUpperLimit", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除兑换铸造上限", notes = "兑换铸造上限", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result dealExchangeCastUpperLimit(@Valid @RequestBody IdRequest request) {
        ecologyCastExchangeConfService.dealExchangeCastUpperLimit(request.getId());
        return Result.success();
    }


    @PostMapping(value = "/getExchangeCastUpperLimitConf", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取兑换配置列表", notes = "获取兑换配置列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<List<AdminExchangeCastUpperLimitConfVO>> getExchangeCastUpperLimitConf(@Valid @RequestBody IdRequest req) {
        return PageResult.success(ecologyCastExchangeConfService.getExchangeCastUpperLimitConf(req.getId()));
    }

    @PostMapping(value = "/updateStatus", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "更新兑换配置状态", notes = "更新兑换配置状态", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result updateStatus(@Valid @RequestBody StatusRequest request) {
        ecologyCastExchangeConfService.updateStatus(request.getId(),request.getStatus());
        return PageResult.success();
    }


    @PostMapping(value = "/initEcologyWayConf", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "初始化铸造方式配置表", notes = "初始化铸造方式配置表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result initEcologyWayConf() {
        SysMember member = SysMemberPool.getMember();
        if (!member.getId().equals(86L)) {
            throw new GlobalRunTimeException("无权限");
        }
        RLock lock = redisson.getLock("script" + member.getId());
        try {
            boolean res = lock.tryLock();
            if (res) {
                ecologyCastExchangeConfService.initEcologyWayConf();
            } else {
                return Result.fail("初始化铸造方式配置表数据同步已经开始：同步人ID：" + member.getId());
            }
        } catch (Exception e) {
            throw new GlobalRunTimeException("初始化铸造方式配置表数据处理异常" + e.getMessage());
        } finally {
            lock.unlock();
        }
        return Result.success();
    }

}
